<?php

class ChatModel
{
    private $chatModelKeyPrefix =   'C/';

    public function postChat($info)
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'CM001';
            return  FALSE;
        }
    	$info['content'] = UK::getstr($info['content'], 250);
		$info['content']  = UK::removeBadLanguage($info['content']);
		$info['nickname'] = UK::getstr($info['nickname'], 24, '', 1);
    	if (strlen($info['content']) < 1 || strlen($info['nickname']) < 1){
            AC::$errorNo    =   'CM001';
            return  FALSE;
        }

        $data['uid']        =   $info['uid'];
        $data['nickname']   =   $info['nickname'];
        $data['content']    =   $info['content'];
        $data['time']       =   AC::SERVER_TIME;

        isset($info['device'])  &&  $data['device'] =   $info['device'];

        $key    =   $this->chatModelKeyPrefix. 0;
        $result =   Storage::addData($key, $data);
        return  $result;
    }

    public function getChatList($info)
    {
        $start  =   0;
        $length =   AC::CHAT_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];
        if ($start  <   0)  $start  =   0;

        $where  =   NULL;
        if (!empty($info['id']))    $where  =   ' id > '. $info['id'];
        if (!empty($info['uid']))
        {
            if (empty($where))
                $where  =   array('uid' => $info['uid']);
            else
                $where  .=  ' AND uid = "'. $info['uid']. '"';
        }
        $orderBy=   ' id DESC ';
        $key    =   $this->chatModelKeyPrefix. 0;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }

    public function getChatListUp($info)
    {
        if (empty($info['id']))
        {
            return  $this->getChatList($info);
        }

        $start  =   0;
        $length =   AC::CHAT_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];
        if ($start  <   0)  $start  =   0;

        $orderBy=   ' id ASC ';
        $where  =   ' id > '. $info['id'];
        if (!empty($info['uid']))   $where  .=  ' AND uid = "'. $info['uid']. '"';
        $key    =   $this->chatModelKeyPrefix. 0;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }

    public function getChatListDown($info)
    {
        if (empty($info['id']))
        {
            return  $this->getChatList($info);
        }

        $start  =   0;
        $length =   AC::CHAT_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];
        if ($start  <   0)  $start  =   0;

        $orderBy=   ' id DESC ';
        $where  =   ' id < '. $info['id'];
        if (!empty($info['uid']))   $where  .=  ' AND uid = "'. $info['uid']. '"';
        $key    =   $this->chatModelKeyPrefix. 0;
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }

    public function removeChat($info)
    {
        if (empty($info['id']))
        {
            AC::$errorNo    =   'CM002';
            return  FALSE;
        }

        $key    =   $this->chatModelKeyPrefix. $info['id'];
        $where  =   array('id'  =>  $info['id']);
        $result =   Storage::delConditionData($key, $where);

        return  $result;
    }

    public function modifyChat($info)
    {
        if (empty($info['id']))
        {
            AC::$errorNo    =   'CM002';
            return  FALSE;
        }

        $data   =   array('content' =>  $info['content']);
        $key    =   $this->chatModelKeyPrefix. $info['id'];
        $where  =   array('id'  =>  $info['id']);
        $result =   Storage::setConditionData($key, $data, $where);

        return  $result;
    }

    public function getDailyChatNum($info)
    {
        $key    =   $this->chatModelKeyPrefix. 0;
        $dbSql  =   ' SELECT count(*) as num, FROM_UNIXTIME(`time`, "%Y%m%d") as ymd FROM chat_0 GROUP BY ymd ';
        $result =   Storage::fetchDbSentence($key, $dbSql, 2);

        return  $result;
    }

    public function getDailyUidNum($info)
    {
        $key    =   $this->chatModelKeyPrefix. 0;
        $dbSql  =   ' SELECT count(*) as num, FROM_UNIXTIME(`time`, "%Y%m%d") as ymd FROM (SELECT uid, time FROM chat_0 GROUP BY uid) as tmp GROUP BY ymd ';
        $result =   Storage::fetchDbSentence($key, $dbSql, 2);

        return  $result;
    }

}


